<?php
class Model_Groups extends Zend_Db_Table {
	
	/**
	 * name of the table
	 *
	 * @var string
	 */
		
	protected $_name = 'groups';
	
	public function init() {}
	
	public function get($filters, $order = null) {
		$select = $this->select();
		
    	foreach($filters AS $key => $value) {
    		
    		// If is array, IN()
    		if(is_array($value)) {
    			$temp = '';
    			foreach($value AS $val) {
    				$temp .= '"' . $val . '",';
    			}
    			$temp = trim($temp, ',');
    			
    			$select->where('LOWER(' . $key . ') IN ('.$temp.')');
    		
    		// Else	
    		} else {
				$select->where('LOWER(' . $key . ') = ?', $value);
			}
    	}
			
		if(!is_null($order)) {
			$select->order($order);
		}
		
		$result_temp = $this->fetchAll($select);
		
		$result = Model_Tools::stripslashes($result_temp);
		
    	return $result;
	}
	
	public function getUsers($group, $filter = null, $order = null) {
		if(is_null($group)) { return array(); }
		
		// check group data
		if(is_numeric($group)) {
			$field = 'id';
		} else {
			$field = 'keyname';
		}
		$select = $this->select('ui.*')
						->setIntegrity(false)
						->join(array('hl_users_datas' => 'ui'), 'ui.id_user = '.$this->_name.'.id_user')
						->where($this->_name.'.'.$field.' = ?', $id_group);
		foreach($filters AS $key => $value) {
    		
    		// If is array, IN()
    		if(is_array($value)) {
    			$temp = '';
    			foreach($value AS $val) {
    				$temp .= '"' . $val . '",';
    			}
    			$temp = trim($temp, ',');
    			
    			$select->where('LOWER(ui.' . $key . ') IN ('.$temp.')');
    		
    		// Else	
    		} else {
				$select->where('LOWER(ui.' . $key . ') = ?', $value);
			}
    	}
    	
    	if(!is_null($order)) {
			$select->order($order);
		}
    	
		$result_temp = $this->fetchAll($select);
		
		$result = Model_Tools::stripslashes($result_temp);
		
    	return $result;
	}
	
	

}
